//
//  YJMUserAccount.swift
//  Weibo
//
//  Created by 杨军民 on 16/7/21.
//  Copyright © 2016年 杨军民. All rights reserved.
//

import UIKit

/*
1、添加user头像和user用户名称
a、添加归档和接档
*/

//用户模型
class YJMUserAccount: NSObject, NSCoding {
    //用于获取用户数据
    var access_token : String?
    
    //计算时间,单位秒数
    var expires_in: NSTimeInterval = 0 {
        didSet{
            //计算时间
            expeires = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    
    //过期时间
    var expeires :  NSDate?
    
    //获取用户的UID
    var uid : String?
    
    //user名称
    var screen_name : String?
    
    //user头像
    var avatar_large :String?
    
    init(dic: [String: AnyObject]) {
        
        //对象需要在对象创建完成之后才能调用
        //当数据没有值的话就会报错
        super.init()
        //KVC
        setValuesForKeysWithDictionary(dic)
    }
    
    //当获取到的数据没有全部使用，当你运行的时候就会导致系统崩溃
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        print("forUndefinedKey:\(key)")
    }
    
    //打印对象
    override var description : String {
        get{
            return "access_token:\(access_token), expeires:\(expeires), UID\(uid)"
        }
    }
    
    //MARK: - 归档、接档
    required init?(coder aDecoder : NSCoder) {
        
        super.init()
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        expeires = aDecoder.decodeObjectForKey("expeires") as? NSDate
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
    }
    
    func encodeWithCoder(aCoder : NSCoder) {
        aCoder.encodeObject(access_token, forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(expeires, forKey: "expeires")
        aCoder.encodeObject(uid, forKey: "uid")
        aCoder.encodeObject(avatar_large,forKey: "avatar_large")
        aCoder.encodeObject(screen_name,forKey: "screen_name")
    }
    
}
